/*
 * bsp_sysIrq.c
 *
 *  Created on: 2023年8月21日
 *      Author: mcealt_DS
 */

#include "bsp_sysIrq.h"
#include "bsp_usart.h"


#define BSP_SYSIRQ_PRINTF  DS_debug_PRINTF_B//阻塞串口打印


void debugHardfault(uint32_t *sp)
{
    uint32_t cfsr  = SCB->CFSR;
    uint32_t hfsr  = SCB->HFSR;
    uint32_t mmfar = SCB->MMFAR;
    uint32_t bfar  = SCB->BFAR;

    uint32_t r0  = sp[0];
    uint32_t r1  = sp[1];
    uint32_t r2  = sp[2];
    uint32_t r3  = sp[3];
    uint32_t r12 = sp[4];
    uint32_t lr  = sp[5];
    uint32_t pc  = sp[6];
    uint32_t psr = sp[7];
#ifdef HUB_DEVICE
    BSP_SYSIRQ_PRINTF("HardFault:\n");
    BSP_SYSIRQ_PRINTF("SCB->CFSR   0x%08lx\n", cfsr);
    BSP_SYSIRQ_PRINTF("SCB->HFSR   0x%08lx\n", hfsr);
    BSP_SYSIRQ_PRINTF("SCB->MMFAR  0x%08lx\n", mmfar);
    BSP_SYSIRQ_PRINTF("SCB->BFAR   0x%08lx\n", bfar);
    BSP_SYSIRQ_PRINTF("\n");

    BSP_SYSIRQ_PRINTF("SP          0x%08lx\n", (uint32_t)sp);
    BSP_SYSIRQ_PRINTF("R0          0x%08lx\n", r0);
    BSP_SYSIRQ_PRINTF("R1          0x%08lx\n", r1);
    BSP_SYSIRQ_PRINTF("R2          0x%08lx\n", r2);
    BSP_SYSIRQ_PRINTF("R3          0x%08lx\n", r3);
    BSP_SYSIRQ_PRINTF("R12         0x%08lx\n", r12);
    BSP_SYSIRQ_PRINTF("LR          0x%08lx\n", lr);
    BSP_SYSIRQ_PRINTF("PC          0x%08lx\n", pc);
    BSP_SYSIRQ_PRINTF("PSR         0x%08lx\n", psr);
#else
    BSP_SYSIRQ_PRINTF("HF:\n");
    BSP_SYSIRQ_PRINTF("1:0x%lx\n", cfsr);
    BSP_SYSIRQ_PRINTF("2:0x%lx\n", hfsr);
    BSP_SYSIRQ_PRINTF("3:0x%lx\n", mmfar);
    BSP_SYSIRQ_PRINTF("4:0x%lx\n", bfar);
    BSP_SYSIRQ_PRINTF("\n");

    BSP_SYSIRQ_PRINTF("5:0x%lx\n", (uint32_t)sp);
    BSP_SYSIRQ_PRINTF("6:0x%lx\n", r0);
    BSP_SYSIRQ_PRINTF("7:0x%lx\n", r1);
    BSP_SYSIRQ_PRINTF("8:0x%lx\n", r2);
    BSP_SYSIRQ_PRINTF("9:0x%lx\n", r3);
    BSP_SYSIRQ_PRINTF("A:0x%lx\n", r12);
    BSP_SYSIRQ_PRINTF("B:0x%lx\n", lr);
    BSP_SYSIRQ_PRINTF("C:0x%lx\n", pc);
    BSP_SYSIRQ_PRINTF("D:0x%lx\n", psr);
#endif
}

__attribute__( (naked) )
void HardFault_Handler(void)
{
    __asm volatile
    (
        "tst lr, #4                                    \n"
        "ite eq                                        \n"
        "mrseq r0, msp                                 \n"
        "mrsne r0, psp                                 \n"
        "ldr r1, debugHardfault_address                \n"
        "bx r1                                         \n"
        "debugHardfault_address: .word debugHardfault  \n"
    );
    NVIC_SystemReset();
    while (1);
}

void BusFault_Handler(void)
{
    uint8_t BUFF[] = "BusFault\r\n";
    BSP_SYSIRQ_PRINTF(BUFF);
    NVIC_SystemReset();
    while (1);
}
void UsageFault_Handler(void)
{
    uint8_t BUFF[] = "UsageFault\r\n";
    BSP_SYSIRQ_PRINTF(BUFF);
    NVIC_SystemReset();
    while (1);
}
void SecureFault_Handler(void)
{
    uint8_t BUFF[] = "SecureFault\r\n";
    BSP_SYSIRQ_PRINTF(BUFF);
    NVIC_SystemReset();
    while (1);
}
